﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WISD.Common
{
    /// <summary>
    /// 类    名：DirFileHelper
    /// 描    述：目录及文件操作
    /// 创建时间：2010/12/22
    /// </summary>
    public static class DirFileHelper
    {
        #region 目录操作
        /// <summary>
        /// 获取当前工作目录
        /// </summary>
        /// <returns></returns>
        public static string GetCurDirectoryPath()
        {
            return System.IO.Directory.GetCurrentDirectory();
        }


        /// <summary>
        /// 获取当前应用程序域目录
        /// </summary>
        /// <returns></returns>
        public static string GetAppDomainPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }


        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <returns>存在：true；不存在：false</returns>
        public static bool IsExistDir(string path)
        {
            try
            {
                return Directory.Exists(path);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDir(string path)
        {
            if (!IsExistDir(path))
            {
                Directory.CreateDirectory(path);
            }
        }


        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteDir(string path)
        {
            if (IsExistDir(path))
            {
                Directory.Delete(path);
            }
        }
        #endregion

        #region 文件操作
        /// <summary>
        /// 文件锁
        /// </summary>
        private static object lockerForFile = new object();


        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool IsExistFile(string file)
        {
            try
            {
                return File.Exists(file);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 创建文件
        /// 有则覆盖，没有则创建
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="name">文件名</param>
        /// <param name="content">文件内容</param>
        public static void CreateFile(string path, string name, string content)
        {
            lock (lockerForFile)
            {
                //创建目录
                if (path.Trim() != "")
                {
                    CreateDir(path);
                }

                FileStream fs = new FileStream(System.IO.Path.Combine(path, name), FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(content);
                sw.Close();
                fs.Close();
            }
        }


        /// <summary>
        /// 创建文件
        /// 有则续写，没有则创建
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="name">文件名</param>
        /// <param name="content">文件内容</param>
        public static void CreateOrOpenFile(string path, string name, string content)
        {
            lock (lockerForFile)
            {
                //创建目录
                if (path.Trim() != "")
                {
                    CreateDir(path);
                }

                FileStream fs = new FileStream(System.IO.Path.Combine(path, name), FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(content);
                sw.Close();
                fs.Close();
            }
        }


        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string ReadFile(string file)
        {
            //判断是否存在
            if (IsExistFile(file))
            {
                StreamReader reader = new StreamReader(file);
                string key = reader.ReadToEnd();
                reader.Close();
                return key;
            }
            else
            {
                throw new Exception("找不到文件：" + file);
            }
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="file"></param>
        public static void DeleteFile(string file)
        {
            if (IsExistFile(file))
            {
                System.IO.File.Delete(file);
            }
        }
        #endregion
    }
}
